// pages/danmu/danmu.js
const {getRandomColor} = require('../../utils/index.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    video:{
      poster:'https://tcb-1251009918.cos.ap-guangzhou.myqcloud.com/demo/video.png',
      src:'http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400',
      danmuList:[
        {
          text:'压缩包文件',
          color:'#ff0000',
          time:1
        },
        {
          text:'蒋总收人头了',
          color:'#ee9900',
          time:2
        },
        {
          text:'哈哈哈哈哈',
          color:'#00ff00',
          time:3
        }
        
      ]
    },
    controls:true,
    loop:false,
    showprogress:true,
    autoplay:false,
    muted:false,
    time:0
  },
  playVideo(){
    // 找到这个视频？
    this.getPlayedTimeByStorage();
    // console.log(this.storageCurrentTime);
    if(this.storageCurrentTime){
      this.videoCtx.seek(this.storageCurrentTime);
    }
    this.videoCtx.play();
  },
  pauseVideo(){
    // 找到这个视频？
    this.videoCtx.pause();
  },
  stopVideo(){
    // 找到这个视频？
    this.videoCtx.stop();
  },
  seekVideo(){
    this.videoCtx.seek(120);
  },
  playRate(){
    this.videoCtx.playbackRate(2.0);
  },
  gotoIndex(){
    wx.navigateTo({
      url: '../index/index',
    })
  },
  bindtime(e){
    // console.log(e);
    // 性能问题
    // this.setData({
    //   time:e.detail.currentTime
    // })
    this.currentTime=e.detail.currentTime;
  },
  sendDanmu(e) {
    // console.log(e, '----------');
    const danmu = e.detail.value.danmu;
    const time = this.currentTime;
    this.videoCtx.sendDanmu({
      text: danmu,
      color: getRandomColor(),
      time: Math.ceil(time)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.videoCtx=wx.createVideoContext('myVideo');
  },
  getPlayedTimeByStorage(){
    this.storageCurrentTime = wx.getStorageSync('playedTime') ? wx.getStorageSync('playedTime') : 0;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log(this.currentTime);
    wx.setStorageSync(`playedTime-&{}`, this.currentTime)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('-----');
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})